//
//  BalanceViewController.swift
//  newShaohuoRen
//
//  Created by hujinbiao on 2018/3/24.
//  Copyright © 2018年 hujinbiao. All rights reserved.
//

import UIKit

class BalanceViewController: UITableViewController {
    
    @IBOutlet weak var totalBalanceLabel: UILabel!
    @IBOutlet weak var balanceLabel: UILabel!
    @IBOutlet weak var withdrawalsLabel: UILabel!
    @IBOutlet weak var frozenLabel: UILabel!
    
    var withdrawStr : String?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "我的钱包"
        
        requestGetUserBalance()
    }
    
    //用户余额
    func requestGetUserBalance() -> Void {
        
        //要加密的数据
        let param = ["command":"getUserBalance","token":UserTicketModel.sharedInstance.token,"uid":UserTicketModel.sharedInstance.uid,"app_name":"shr"]
        
        NetworkTools.sharedInstance.postRequest(urlString: "", parameters: param as AnyObject, headers: ["copyright":"v1","interface":"UserInfo"], successed: { (resultDict) in
            
            let data = resultDict["data"] as! [String:AnyObject]
            self.totalBalanceLabel.text = "￥\(data["total_balance"]!)"
            self.withdrawStr = data["withdraw_balance"] as? String
            self.balanceLabel.text = "￥\(data["recharge_balance"]!)"
            self.withdrawalsLabel.text =  "可提现金额￥\(self.withdrawStr!)"
            self.frozenLabel.text = "￥\(data["frozen_balance"]!)"
        })
    }
    
    //提现
    @IBAction func withdrawalsBtnAction(_ sender: UIButton) {
        let board = UIStoryboard(name: "WithdrawalsController", bundle: nil)
        let Vc = board.instantiateInitialViewController() as! WithdrawalsController
        Vc.balanceStr = self.withdrawStr!
        navigationController?.pushViewController(Vc, animated: true)
    }
    
    //充值
    @IBAction func rechargeBtnAction(_ sender: UIButton) {
        let board = UIStoryboard(name: "ChoiceController", bundle: nil)
        let Vc = board.instantiateInitialViewController() as! ChoiceController
        Vc.sign = "recharge"
        Vc.paymentMoney = self.balanceLabel.text!
        navigationController?.pushViewController(Vc, animated: true)
    }
    
}

extension BalanceViewController {
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section==1{
            if indexPath.row==0{
                let transaVc = TransactionDetailsController()
                navigationController?.pushViewController(transaVc, animated: true)
            }
        }
    }
}
